<?php
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends BaseController {
    public function article_list(){
        $this->display();
    }
    public function article_ajax(){
        //接收搜索的值
        $search=I('get.value');
        //分页
        //每页显示的数据
        $limit=5;
        //查询数据总数
        if(empty($search)){
            //查询所有数据
            $count=M('article')->count();
        }else{
            $count=M('article')->where("art_title Like '%$search%'")->count();
        }
        //调用TP分页类
        $page=new \Think\Page($count,$limit);
        //生成分页
        $show=$page->show();

        //排序判断
        $ord=I('get.ord');//接收排序ord
        if($ord==1){
            $order="art_pageview ASC,";
        }else{
            $order="art_pageview DESC,";
        }
        //查询数据
        if(empty($search)){
            //如果没有输入搜索
            $article=M('article')
                    ->order("{$order}art_addtime DESC")
                    ->limit($page->firstRow.','.$page->listRows)
                    ->select();
        }else{
            $article=M('article')
                    ->where("art_title Like '%$search%'")
                    ->order("{$order}art_addtime DESC")
                    ->limit($page->firstRow.','.$page->listRows)
                    ->select();
        }
        $b=0;
        $this->assign('b',$b);
        $this->assign('show',$show);
        $this->assign('article',$article);
        $this->assign('a',$page->firstRow+1);


        $this->display();
    }
    public function article_ajax_show(){
        //处理显示ajax的数据
        if(!empty($_GET['show'])){
            $id=$_GET['id'];
            $show=$_GET['show'];
            if($show==1){
                $data=array(
                    'is_show'=>2,
                );
            }else{
                $data=array(
                    'is_show'=>1,
                );
            }
			$res=M('article')->where("art_id=$id")->save($data);
            if($res){
                echo 1;die;
            }else{
                echo 2;die;
            }
        }
        $this->display();
    }

    public function article_ajax_title(){
        //处理标题ajax的数据
        if(!empty($_GET['value'])){
            $id=$_GET['id'];
            $value=$_GET['value'];
            $data=array(
                'art_title'=>$value,
            );
            $res=M('article')->where("art_id=$id")->save($data);
            if($res){
                echo 1;die;
            }else{
                echo 2;die;
            }
        }
        $this->display();
    }

    //添加文章
    public function article_add(){
        $art_addtime=!empty($_POST['art_addtime'])?strtotime($_POST['art_addtime']):time();

        if(I('post.')){
            $data=array(
                'art_title'=>I('post.art_title'),
                'art_author'=>I('post.art_author'),
                'art_content'=>I('post.art_content'),
                'art_addtime'=>$art_addtime,
                'art_pageview'=>I('post.art_pageview'),
            );
            //上传图片
            $upload=new \Think\Upload();
            $upload->exts=array('jpg','png','gif');
            $upload->rootPath='./Upload/';
            $upload->savePath='Article/';
            $info=$upload->upload();
            if($info){
                $data['art_img']=$info['art_img']['savepath'].$info['art_img']['savename'];
            }
            // p($data);
            $res=M('article')->add($data);
            if($res){
                $this->success('添加成功',U('Admin/Article/article_list'));
                exit();
            }else{
                $this->error('数据输入有误');
                exit();
            }
        }
        $this->display();
    }
    
    //编辑文章
    public function article_edit(){
        //接收参数
        $aid=I('get.aid');
        $article=M('article')->where("art_id=$aid")->find();
        $this->assign('article',$article);
        
        //获取旧图片
        $old_arr=M('article')->field('art_img')->where("art_id=$aid")->find();
        $path='./Upload/'.$old_arr['art_img'];
        $this->assign('path',$path);
        
        if(I('post.')){
            $art_addtime=!empty($_POST['art_addtime'])?strtotime($_POST['art_addtime']):time();
            $data=array(
                'art_title'=>I('post.art_title'),
                'art_author'=>I('post.art_author'),
                'art_content'=>I('post.art_content'),
                'art_addtime'=>$art_addtime,
                'art_pageview'=>I('post.art_pageview'),
            );
            //接收隐藏域的ID
            $art_id=I('post.art_id');

            if($_FILES['art_img']['size']){//如果有图片上传
                //删除旧图片
                //判断字段不为空并且图片存在
                if(!empty($old_arr) && file_exists($path)){
                    unlink($path);
                }
                //上传图片
                $upload=new \Think\Upload();
                $upload->exts=array('jpg','png','gif');
                $upload->rootPath='./Upload/';
                $upload->savePath='Article/';
                $info=$upload->upload();
                if($info){
                    $data['art_img']=$info['art_img']['savepath'].$info['art_img']['savename'];
                }
            }
			$res=M('article')->where("art_id=$art_id")->save($data);
            if($res){
                $this->success('编辑成功',U('Admin/Article/article_list'));
                exit();
            }else{
                $this->error('数据输入有误');
                exit();
            }
        }

        $this->display();
    }

    public function article_del(){
        $a_id=I('get.a_id');
        //删除旧图片
        $old_arr=M('article')->field('art_img')->where("art_id=$a_id")->find();
        $path='./Upload/'.$old_arr['art_img'];
        //判断字段不为空并且图片存在
        if(!empty($old_arr) && file_exists($path)){
            unlink($path);
        }
        //删除数据
        $res=M('article')->where("art_id=$a_id")->delete();
        if($res){
            echo 1;exit();
        }else{
            echo M('article')->getLastSql();exit();
        }
        
        $this->display();                                      
    }

    
}